using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using dao = Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Common;
using Microsoft.ConnectionEngine.Common.Security;
using Microsoft.ConnectionEngine.Common;

namespace Microsoft.ConnectionEngine.Administration.Core.ServiceProvider.Business
{
    /// <summary>
    /// Exposes methods that allow communication with the Routing Services
    /// </summary>
    public class ServiceProviderManager
    {
        /// <summary>
        /// Get a list of providers by search criteria 
        /// </summary>
        /// <param name="searchString">If left empty (empty string) all  providers are returned</param>
        /// <returns></returns>
        public static dao.ServiceProviders Search(HSDPrincipal currentPrincipal, dao.ServiceProviderSearchRequest searchRequest)
        {
            //viewable by all users
            string searchRequestXml = AdapterHelper.ConstructObjectXml(searchRequest, typeof(dao.ServiceProviderSearchRequest));
            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, searchRequestXml);

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                AdapterHelper.GetMessageTypeValue(dao.MessageTypes.ServiceProviderSearchRequest), ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);

            return (dao.ServiceProviders)AdapterHelper.GetPayloadObjectFromResponse(result, typeof(dao.ServiceProviders));                        
        }     

        /// <summary>
        /// Retrieves a ServiceProvider for read only
        /// </summary>
        public static dao.ServiceProvider SelectItem(HSDPrincipal currentPrincipal, int serviceProviderID)
        {         
            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, "ServiceProviderID", serviceProviderID.ToString());

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                1070, ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);
            return (dao.ServiceProvider)AdapterHelper.GetPayloadObjectFromResponse(result, typeof(dao.ServiceProvider));
        }
        
        /// <summary>
        /// Locks a ServiceProvider for the current user
        /// </summary>
        /// <param name="serviceProviderID"></param>
        /// <returns>SeviceProvider - If checkout successful, null otherwise </returns>
        public static dao.ServiceProvider Lock(HSDPrincipal currentPrincipal, string serviceProviderID)
        {
             if (!SecurityManager.ProviderRights(currentPrincipal, serviceProviderID))
                throw new SecurityException();

            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, "ServiceProviderID", serviceProviderID.ToString());

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                AdapterHelper.GetMessageTypeValue(dao.MessageTypes.ServiceProviderLock), ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);
            return (dao.ServiceProvider)AdapterHelper.GetPayloadObjectFromResponse(result, typeof(dao.ServiceProvider));           
        }

        /// <summary>
        /// Unlocks a ServiceProvider locked by the current user
        /// </summary>
        /// <param name="serviceProviderID"></param>
        /// <returns>True - If checkout successful, false otherwise </returns>
        public static bool Unlock(HSDPrincipal currentPrincipal, string serviceProviderID)
        {
            if (!SecurityManager.ProviderRights(currentPrincipal,serviceProviderID))
                throw new SecurityException();

            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, "ServiceProviderID", serviceProviderID.ToString());

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                AdapterHelper.GetMessageTypeValue(dao.MessageTypes.ServiceProviderUnlock), ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);
            
            return AdapterHelper.GetBooleanFromResponse(result);  
        }
        /// <summary>
        /// Unlocks all ServiceProviders locked by the current user
        /// </summary>
        /// <returns>True - If successful, false otherwise</returns>
        public static bool UnlockAll(HSDPrincipal currentPrincipal)
        {
            if (!SecurityManager.UserInRoles(currentPrincipal, new String[2] { RoleTypes.Administrator, RoleTypes.Provider }))
                throw new SecurityException();

            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, "ServiceProviderUnlockAll", "");

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                AdapterHelper.GetMessageTypeValue(dao.MessageTypes.ServiceProviderUnlockAll), ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);
            
            return AdapterHelper.GetBooleanFromResponse(result);  
        }


        /// <summary>
        /// Add a new service provider to health service directory
        /// </summary>
        /// <param name="provider"></param>
        /// <returns>True - If successful, false otherwise</returns>
        /// <remarks>the field ServiceProviderID value can not be left empty when calling this method. It is recommended to ser the value of 
        /// ServiceProviderID to int.MinValue when creating a new provider. A valid ID will be assign to the provider by HSD</remarks>
        public static string Insert(HSDPrincipal currentPrincipal, dao.ServiceProvider provider)
        {
            if (!currentPrincipal.IsInRole(RoleTypes.Administrator))
                throw new SecurityException();

            string providerXml = AdapterHelper.ConstructObjectXml(provider, typeof(dao.ServiceProvider));
            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, providerXml);

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                AdapterHelper.GetMessageTypeValue(dao.MessageTypes.ServiceProviderInsert), ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);
        
            return ((dao.ServiceProvider)AdapterHelper.GetPayloadObjectFromResponse(result, typeof(dao.ServiceProvider))).ServiceProviderID;   
        }

        /// <summary>
        /// Update an existing service provider
        /// </summary>
        /// <param name="provider"></param>
        /// <returns></returns>
        public static bool Update(HSDPrincipal currentPrincipal, dao.ServiceProvider provider)
        {
            if (!SecurityManager.ProviderRights(currentPrincipal,provider.ServiceProviderID))
                throw new SecurityException();

            string providerXml = AdapterHelper.ConstructObjectXml(provider, typeof(dao.ServiceProvider));
            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, providerXml);

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                AdapterHelper.GetMessageTypeValue(dao.MessageTypes.ServiceProviderUpdate), ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);
            return AdapterHelper.GetBooleanFromResponse(result);              
        }

        /// <summary>
        /// Delete service provider
        /// </summary>
        /// <param name="providerID"></param>
        /// <returns></returns>
        public static bool Delete(HSDPrincipal currentPrincipal, string serviceProviderID)
        {
            if (!SecurityManager.ProviderRights(currentPrincipal, serviceProviderID))
                throw new SecurityException();

            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, "ServiceProviderID", serviceProviderID.ToString());

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                AdapterHelper.GetMessageTypeValue(dao.MessageTypes.ServiceProviderDelete), ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);

            return AdapterHelper.GetBooleanFromResponse(result);  
        }
    }
}
